#include "system.h"
#include "BspLed.h"

#include "string.h"


#include "MotorsTask.h"
#include "can_task.h"
#include "surface.h"


#define APP_TASK_STACK_SIZE  (256)  // APP 任务堆栈宏
#define APP_TASK_STACK_PRIORITY  (1)  // APP 任务优先级

#define LED_TASK_STACK_SIZE  (256)  // LED 任务堆栈宏
#define LED_TASK_STACK_PRIORITY  (1)  // LED 任务优先级

#define MOTORS_TASK_STACK_SIZE  (2048)  // 电机 任务堆栈宏
#define MOTORS_TASK_STACK_PRIORITY  (3)  // 电机 任务优先级

#define CAN_TASK_STACK_SIZE  			(2048)  // CAN 任务堆栈宏
#define CAN_TASK_STACK_PRIORITY  	(2)  // CAN 任务优先级

uint32_t GetFreeHeapSize = 0;
uint32_t EverFreeHeapSize = 0;

volatile uint64_t SystemTime = 0;

/*********************************************************************************************
  * @brief  LedTask  led 任务
  * @param  void *pvParameters 任务参数指针
  * @retval None
	* @IO None
  ********************************************************************************************/
void LedTask(void *pvParameters)
{
	uint32_t LedCount = 0;
  LED_Init();
	while(1)
	{
		SystemTime+=10;
		LedCount++;
		surface_timer(SystemTime);
		if(LedCount % 25 == 0)
		{
			LED_RUN_Reverse();
		}
		GetFreeHeapSize = xPortGetFreeHeapSize();
		EverFreeHeapSize = xPortGetMinimumEverFreeHeapSize();
		vTaskDelay(10);
	}
}

/*********************************************************************************************
  * @brief  AppTask  App 任务
  * @param  void *pvParameters 任务参数指针
  * @retval None
	* @IO None
  ********************************************************************************************/
void AppTask(void *pvParameters)
{
  BaseType_t Status;
	
	
/******************************* 创建 LED 任务 *******************************/
	Status = xTaskCreate(LedTask,\
	                      "LedTask",\
	                      LED_TASK_STACK_SIZE,\
	                      NULL,\
	                      LED_TASK_STACK_PRIORITY,\
	                      NULL);
	if(Status != pdPASS)
		 while(1);
	
	if(Status != pdPASS)
		 while(1);

/******************************* 创建 Motors 任务 *******************************/
	Status = xTaskCreate(MotorsTask,\
	                      "MotorsTask",\
	                      MOTORS_TASK_STACK_SIZE,\
	                      NULL,\
	                      MOTORS_TASK_STACK_PRIORITY,\
	                      NULL);
	if(Status != pdPASS)
		 while(1);
	
	if(Status != pdPASS)
		 while(1);
	
/******************************* 创建 CAN 任务 *******************************/
	Status = xTaskCreate(CanTask,\
	                      "CanTask",\
	                      CAN_TASK_STACK_SIZE,\
	                      NULL,\
	                      CAN_TASK_STACK_PRIORITY,\
	                      NULL);
	if(Status != pdPASS)
		 while(1);
	
	if(Status != pdPASS)
		 while(1);
	
	
	vTaskDelete(NULL);  // 删除任务
}
/*********************************************************************************************
  * @brief  AppInit   初始化任务 
  * @param  None
  * @retval uint8_t  0 成功    1 失败
	* @IO None
  ********************************************************************************************/
uint8_t AppInit(void)
{
	BaseType_t xReturn;
	xReturn = xTaskCreate(AppTask,\
	                      "AppTask",\
	                      APP_TASK_STACK_SIZE,\
	                      NULL,\
	                      APP_TASK_STACK_PRIORITY,\
	                      NULL);
	if(xReturn != pdPASS)
		return 1;
	return 0;
}


